package com.ziroom.finance.rabbitmq.common.interceptor;

import com.google.common.collect.Lists;
import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 打印日志的拦截器
 * Created by duxiaokang on 2016/11/21.
 */
public class LogInterceptor {
    private static final Logger logger = LoggerFactory.getLogger(LogInterceptor.class);

    /**
     * 某些方法参数过长,不打日志
     * 比如FileServiceImpl.upload文件上传,参数是byte数组
     */
    private static final List<String> NOT_LOGGING_LIST = Lists.newArrayList(
            "FileServiceImpl.upload"
    );

    /**
     * 对于service的处理
     * @param joinPoint 可执行的连接点
     * @return ret
     * @throws Throwable
     * @author duxiaokang
     */
    public Object action(ProceedingJoinPoint joinPoint) throws Throwable {
        String className = joinPoint.getTarget().getClass().getSimpleName();
        String method = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        String clzMethod = className + "." + method;
        if (NOT_LOGGING_LIST.contains(clzMethod)) {
            // 直接返回结果
            return joinPoint.proceed();
        }
        logger.info("method {} start, args: {}", clzMethod, args);
        long start = System.currentTimeMillis();
        Object ret = joinPoint.proceed();
        long end = System.currentTimeMillis();
        logger.info("method {} end, cost: {}ms", clzMethod, (end - start));
        return ret;
    }
}
